﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Timer
{
    public partial class Form1 : Form
    {
        //声明当前时间以秒为单位
        private int _nowSecond = 0;
        //定义图片数组
        private Bitmap[] _bmpShow = new Bitmap[13];
        public Form1()
        {
            InitializeComponent();
            _bmpShow[0] = new Bitmap("images/0.bmp");
            _bmpShow[1] = new Bitmap("images/1.bmp");
            _bmpShow[2] = new Bitmap("images/2.bmp");
            _bmpShow[3] = new Bitmap("images/3.bmp");
            _bmpShow[4] = new Bitmap("images/4.bmp");
            _bmpShow[5] = new Bitmap("images/5.bmp");
            _bmpShow[6] = new Bitmap("images/6.bmp");
            _bmpShow[7] = new Bitmap("images/7.bmp");
            _bmpShow[8] = new Bitmap("images/8.bmp");
            _bmpShow[9] = new Bitmap("images/9.bmp");
            _bmpShow[10] = new Bitmap("images/dot1.bmp");
            _bmpShow[11] = new Bitmap("images/dot2.bmp");
            _bmpShow[12] = new Bitmap("images/blank.bmp");
        }
        //获取图片
        private Bitmap getPicture(int num) {
            if (num == 0)
                return _bmpShow[0];
            else if (num == 1)
                return _bmpShow[1];
            else if (num == 2)
                return _bmpShow[2];
            else if (num == 3)
                return _bmpShow[3];
            else if (num == 4)
                return _bmpShow[4];
            else if (num == 5)
                return _bmpShow[5];
            else if (num == 6)
                return _bmpShow[6];
            else if (num == 7)
                return _bmpShow[7];
            else if (num == 8)
                return _bmpShow[8];
            else if (num == 9)
                return _bmpShow[9];
            return _bmpShow[12];

        }
        //点击开始的事件
        private void buttonStart_Click(object sender, EventArgs e)
        {
            //初始化时间
            _nowSecond = 0;
            //启动定时器
            timer1.Enabled = true;
            timer2.Enabled = true;
            //初始化显示值
            pictureBoxColon1.Image = _bmpShow[11];
            pictureBoxColon2.Image = _bmpShow[11];
            pictureBoxHour1.Image = getPicture(0);
            pictureBoxHour2.Image = getPicture(0);
            pictureBoxMinute1.Image = getPicture(0);
            pictureBoxMinute2.Image = getPicture(0);
            pictureBoxSecond1.Image = getPicture(0);
            pictureBoxSecond2.Image = getPicture(0);
            //启用或禁止相关按钮
            buttonPauseContinue.Enabled = true;
            buttonStop.Enabled = true;
            buttonStart.Enabled = false;
            //改变按钮文字内容
            buttonPauseContinue.Text = "暂停";

        }
        //点击暂停/继续
        private void buttonPauseContinue_Click(object sender, EventArgs e)
        {   
            //可以停止，说明在播放，当前按钮是暂停
            if (buttonStop.Enabled == true)
            {
                //关闭定时器
                timer1.Enabled = false;
                timer2.Enabled = false;
                //显示:
                pictureBoxColon1.Image = _bmpShow[11];
                pictureBoxColon2.Image = _bmpShow[11];
                //修改按钮文本
                buttonPauseContinue.Text = "继续";
                //启用或禁止相关按钮
                buttonPauseContinue.Enabled = true;
                buttonStop.Enabled = false;
                buttonStart.Enabled = false;
            }
            //当前按钮是继续,设计暂停状态不能点击停止，用于区分是否是暂停
            else 
            {
                //开启定时器
                timer1.Enabled = true;
                timer2.Enabled = true;
                //显示:
                pictureBoxColon1.Image = _bmpShow[11];
                pictureBoxColon2.Image = _bmpShow[11];
                //修改按钮文本
                buttonPauseContinue.Text = "暂停";
                //启用或禁止相关按钮
                buttonPauseContinue.Enabled = true;
                buttonStop.Enabled = true;
                buttonStart.Enabled = false;
            }
        }
        //点击停止
        private void buttonStop_Click(object sender, EventArgs e)
        {
            //初始化时间
            _nowSecond = 0;
            //关闭定时器
            timer1.Enabled = false;
            timer2.Enabled = false;
            //显示:
            pictureBoxColon1.Image = _bmpShow[11];
            pictureBoxColon2.Image = _bmpShow[11];
            //启用或禁止相关按钮
            buttonPauseContinue.Enabled = false;
            buttonStop.Enabled = false;
            buttonStart.Enabled = true;
            //改变按钮文字内容
            buttonPauseContinue.Text = "暂停";
        }
        //定义定时器事件，每1秒执行一次
        private void timer1_Tick(object sender, EventArgs e)
        {
            //每秒+1(0.1秒为单位)
            _nowSecond++;
            //求出小时，分，秒
            int hour = _nowSecond / 3600;
            int minute = (_nowSecond / 60) % 60;
            int second = _nowSecond % 60;
            int h1 = hour / 10;
            int h2 = hour % 10;
            int m1 = minute / 10;
            int m2 = minute % 10;
            int s1 = second / 10;
            int s2 = second % 10;
            //显示时间
            pictureBoxHour1.Image = getPicture(h1);
            pictureBoxHour2.Image = getPicture(h2);
            pictureBoxMinute1.Image = getPicture(m1);
            pictureBoxMinute2.Image = getPicture(m2);
            pictureBoxSecond1.Image = getPicture(s1);
            pictureBoxSecond2.Image = getPicture(s2);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //每0.5秒切换一次：的显示情况
            if (pictureBoxColon1.Image == _bmpShow[11])
            {
                pictureBoxColon1.Image = _bmpShow[12];
                pictureBoxColon2.Image = _bmpShow[12];
            }
            else
            {
                pictureBoxColon1.Image = _bmpShow[11];
                pictureBoxColon2.Image = _bmpShow[11];
            }
        }
        //窗口加载事件
        private void Form1_Load(object sender, EventArgs e)
        {
            buttonStart.Enabled = true;
            buttonPauseContinue.Enabled = false;
            buttonStop.Enabled = false;
            //设置暂停按钮文字
            buttonPauseContinue.Text = "暂停";
            //:初始可以显示
            pictureBoxColon1.Image = _bmpShow[11];
            pictureBoxColon2.Image = _bmpShow[11];
        }
        //窗体移动事件
        private void Form1_Move(object sender, EventArgs e)
        {
            //停靠在左右边框
            //屏幕右侧距离屏幕左侧的距离
            int scrennRight = Screen.PrimaryScreen.Bounds.Right;
            //窗体右侧到屏幕左侧距离
            int formRight = this.Left + this.Size.Width;
            if (Math.Abs(scrennRight - formRight) <= 100)
                this.Left = scrennRight - this.Size.Width;
            //距离屏幕左侧距离<=100
            if (Math.Abs(this.Left) <= 100)
                this.Left = 0;

            //停靠在上下边框
            //类似于左右
            int scrennBottom = Screen.PrimaryScreen.Bounds.Bottom;
            int formBottom = this.Top + this.Size.Height;
            if (Math.Abs(scrennBottom - formBottom) <= 60)
                this.Top = scrennBottom - this.Size.Height;
            if (Math.Abs(this.Top) <= 100)
                this.Top = 0;
        }
    }
}
